package com.spzx.product.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.service.IBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "品牌管理")
@RestController
@RequestMapping("/brand")
@Slf4j
public class BrandController extends BaseController {

    @Autowired
    IBrandService brandService;

    //接口主要步骤： 1. 接收数据    2.调用业务层     3.封装返回结果
    /**
     * 品牌分页接口
     * @param brand
     * @return
     */
    @Operation(summary = "分页")
    @GetMapping("list")
    @RequiresPermissions("product:brand:list")
    public TableDataInfo list(Brand brand){
        //   ?pageNum=1&pageSize=10
        startPage(); //com.github.pagehelper.PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
        List<Brand> list = brandService.selectBrandList(brand);  //实际返回对象：com.github.pagehelper.Page   extends ArrayList
        return getDataTable(list);
    }

    /**
     * 根据id查询品牌对象
     * @param id 主键
     * @return 品牌对象
     */
    @Operation(summary = "根据id查询品牌对象")
    @GetMapping("/{id}")
    @RequiresPermissions("product:brand:query")
    public AjaxResult selectBrandById(@PathVariable Long id){
        Brand brand = brandService.selectBrandById(id);
        return success(brand);
    }

    /**
     * 添加品牌
     * @param brand 品牌信息
     * @return 添加是否成功
     */
    @Operation(summary = "添加")
    @PostMapping
    @RequiresPermissions("product:brand:add")
    @Log(title = "品牌管理", businessType = BusinessType.INSERT)
    public AjaxResult addBrand(@RequestBody @Validated Brand brand){ //导包不要导错，@RequestBody  org.springframework.web.bind.annotation.RequestBody
        brand.setCreateBy(SecurityUtils.getUsername());
        int rows = brandService.addBrand(brand);
        return toAjax(rows);
    }

    /**
     * 修改品牌
     * @param brand 品牌信息
     * @return 修改是否成功
     */
    @Operation(summary = "修改")
    @PutMapping
    @RequiresPermissions("product:brand:edit")
    @Log(title = "品牌管理", businessType = BusinessType.UPDATE)
    public AjaxResult updateBrand(@RequestBody @Validated Brand brand){ //导包不要导错，@RequestBody  org.springframework.web.bind.annotation.RequestBody
        brand.setUpdateBy(SecurityUtils.getUsername());
        int rows = brandService.updateBrand(brand);
        return toAjax(rows);
    }

    @Operation(summary = "删除")
    @DeleteMapping("/{ids}")  //      http://192.168.6.121/product/brand/1,2,3
    @RequiresPermissions("product:brand:remove")
    @Log(title = "品牌管理", businessType = BusinessType.DELETE)
    public AjaxResult deleteBatch(@PathVariable List<Long> ids){
        int rows = brandService.deleteBatch(ids);
        return toAjax(rows);
    }

    @Operation(summary = "获取全部品牌")
    @GetMapping("getBrandAll")
    @RequiresPermissions("product:brand:query")
    public AjaxResult getBrandAll() {
        return success(brandService.selectBrandAll());
    }

    //=======前台系统使用到的接口=====================================================================
    /**
     * 获取全部品牌
     * @return
     */
    @InnerAuth
    @Operation(summary = "获取全部品牌")
    @GetMapping("getBrandAllList")
    public R<List<Brand>> getBrandAllList() {
        return R.ok(brandService.selectBrandAll());
    }
}
